package examplePackage;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Date;

public class UserDaoImpl implements UserDao {
	static ResultSet rSet = null;

	@Override
	public UserBean login(Connection conn, UserBean user) {

		// preparing some objects for connection
		Statement statement = null;

		String username = user.getUsername();
		String password = user.getPassword();

		String searchQuery = "select * from users where username='" + username + "' AND password='" + password + "'";

		System.out.println("Your user name is " + username);
		System.out.println("Your password is " + password);
		System.out.println("Query: " + searchQuery);

		try {
			// connect ot DB
			statement = conn.createStatement();
			rSet = statement.executeQuery(searchQuery);
			boolean more = rSet.next();
			
//			if (username.equals("极客学院") && password.equals("123456"))	 {
//				more = true;
//			}

			if (!more) {
				System.out.println("Sorry, you are not a registered user!Please sign up first");
				user.setValid(false);
			} else {
				String firstName = rSet.getString("FirstName");
				String lastName = rSet.getString("LastName");

				System.out.println("Welcome " + firstName);
				user.setFirstName(firstName);
				user.setLastName(lastName);
				user.setValid(true);
			}
		} catch (Exception e) {
			System.out.println("Log in failed: An Exception has occurred!" + e);
		} finally {
			if (rSet != null) {
				try {
					rSet.close();
				} catch (Exception e2) {
				}

				rSet = null;

			}

			if (statement != null) {
				try {
					statement.close();
				} catch (Exception e2) {
				}

				statement = null;

			}

			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e2) {
				}

				conn = null;
			}

		}

		return user;

	}

	@Override
	public UserBean register(Connection conn, UserBean user) {

		// preparing some objects for connection
		Statement statement = null;

		String username = user.getUsername();
		String password = user.getPassword();
		String firstname = user.getFirstName();
		String lastname = user.getLastName();
		String email = user.getEmail();
		java.util.Date dt = new java.util.Date();
		Date date = new Date(dt.getTime());
		String updateSql = "insert into users(firstName, lastName, email, username, password, regdate) values ('"
				+ firstname + "','" + lastname + "','" + email + "','" + username + "','" + password + "', '" + date
				+ "')";

		System.out.println("Your user name is " + username);
		System.out.println("Your password is " + password);
		System.out.println("Your first name is " + firstname);
		System.out.println("Your last name is " + lastname);
		System.out.println("Your email is " + email);
		System.out.println("Your register date is " + date.toString());
		System.out.println("updateSql: " + updateSql);

		try {
			statement = conn.createStatement();
			int rt = statement.executeUpdate(updateSql);

			if (rt > 0) {
				user.setValid(true);
			} else {
				user.setValid(false);
			}

		} catch (Exception e) {

			System.out.println("Register failed: An Exception has occurred!" + e);
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (Exception e2) {
				}

				statement = null;

			}

			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e2) {
				}

				conn = null;
			}
		}

		return user;
	}

}
